package com.wsj.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wsj.dao.IBmClassTypeDao;
import com.wsj.dao.ICmQuotedCompanyDao;
import com.wsj.dao.IHomeSiteNewsClassDao;
import com.wsj.service.IHomeSiteNewsClassServ;
import com.wsj.util.exception.BusinessException;
import com.wsj.web.vo.homeSite.BmClassType;
import com.wsj.web.vo.homeSite.BmNewsType;
import com.wsj.web.vo.homeSite.CmQuotedCompany;
import com.wsj.web.vo.homeSite.HomeSiteNewsClass;

public class HomeSiteNewsClassServImpl implements IHomeSiteNewsClassServ{

	private IHomeSiteNewsClassDao homeSiteNewsClassDao;
	
	//分类
	private IBmClassTypeDao bmClassTypeDao; 
	
	//证券代码,因为使用spring,所以暴露这个接口
	private ICmQuotedCompanyDao cmQuotedCompanyDao;
	
	/* 先添加然后再修改 Seq 字段 ,排序字段*/
	public boolean addNewsClass(HomeSiteNewsClass hs) throws BusinessException {
		this.homeSiteNewsClassDao.addNewsClass(hs);
		hs.setSeq(hs.getSeq()+hs.getId()+",");
		Long id = hs.getId();
		hs.setSequence(id.shortValue());
		this.homeSiteNewsClassDao.updateNewsClass(hs);
		return true;
	}

	public boolean deleteNewsClass(Long HomeSiteNewsClassId) throws BusinessException {
		this.homeSiteNewsClassDao.deleteNewsClass(HomeSiteNewsClassId);
		return true;
	}

	public boolean deleteNewsClassByParentId(Long parentId) throws BusinessException {
		this.homeSiteNewsClassDao.deleteNewsClassByParentId(parentId);
		return true;
	}

	public HomeSiteNewsClass loadNewsClassById(Long hsId) throws BusinessException {
		HomeSiteNewsClass hs = new HomeSiteNewsClass();
		hs = this.homeSiteNewsClassDao.loadNewsClassById(hsId);
		return hs;
	}

	/* 根据父类ID、资讯还是广告获取子类集合 */
	public List loadNewsClassList(Long parentId,Integer typeId) throws BusinessException {
		List list = new ArrayList();
		list = this.homeSiteNewsClassDao.loadNewsClassList(parentId,typeId);
		return list;
	}
	
	public boolean updateNewsClass(HomeSiteNewsClass hs) throws BusinessException {
		this.homeSiteNewsClassDao.updateNewsClass(hs);
		return true;
	}

	public List loadAllNewsClass() throws BusinessException {
		List list = new ArrayList();
		list = this.homeSiteNewsClassDao.loadAllNewsClass();
		return list;
	}

	/* 获取字符串的树图 */
	public String getChildStr(Long parentId,Integer typeId, Integer branch, Integer selected) throws BusinessException {
		String str = "";
		List list = new ArrayList();
		list = this.homeSiteNewsClassDao.loadNewsClassList(parentId,typeId);
		str = this.homeSiteNewsClassDao.getChildStr(list,typeId, branch, selected);
		return str;
	}

	/**
	 *   eg:给 ID 三级分类的 7 返回 一二级的 ,1,3 
	 *   在以后的调用时处理成完整的 ,1,3,7  
	 *   返回当前的，id
	 * */
	public String getSeq(Long newsClassId,Long parId) throws BusinessException 
	{
		StringBuffer cur = new StringBuffer("");
		
		if(parId!=-1)
		{
			HomeSiteNewsClass hp = this.homeSiteNewsClassDao.loadNewsClassById(parId); 
			cur = new StringBuffer(hp.getSeq());
		}else{
			cur = new StringBuffer(","+parId+",");
		}
		
		return cur.toString();
	}

	public List loadListByClassTypeId(Integer classTypeId) throws BusinessException {
		
		return this.homeSiteNewsClassDao.loadListByClassTypeId(classTypeId);
	}
	
	/** 
	 * 迁移后，修改符合条件的所有的 seq 字段
	 * eg:把 5 移动到 3下面
	 * parId ",-1,3,"
	 * newClassId "%,5,%"
	 * 移动后结果 "-1,3,5,%" 
	 *  **/
	public boolean updateSeq(Long newsClassId, Long parId) throws BusinessException {
		boolean flag = false;
		flag = this.homeSiteNewsClassDao.updateSeq(newsClassId,parId);
		return flag;
	}

	
	

	public IHomeSiteNewsClassDao getHomeSiteNewsClassDao() {
		return homeSiteNewsClassDao;
	}

	public void setHomeSiteNewsClassDao(IHomeSiteNewsClassDao homeSiteNewsClassDao) {
		this.homeSiteNewsClassDao = homeSiteNewsClassDao;
	}

	public HomeSiteNewsClass loadNewsClassByNames(String names, Integer typeId) throws BusinessException 
	{
		// TODO Auto-generated method stub
		
		HomeSiteNewsClass homeSiteNewsClass=this.homeSiteNewsClassDao.loadNewsClassByNames(names,typeId);
		return homeSiteNewsClass;
	}

	/******
	 * 通过名称检测分了名称是否重复
	 */
	public HomeSiteNewsClass loadNewsClassByNames(String names) throws BusinessException 
	{
		HomeSiteNewsClass homeSiteNewsClass=this.homeSiteNewsClassDao.loadNewsClassByNames(names);
		return homeSiteNewsClass;
	}

	
	/******
	 * 获取父类下属于广告分类的列表 
	 */
	public List loadFirstHomeSiteNewsClassByType(Integer typeId) throws BusinessException 
	{
		return  this.getHomeSiteNewsClassDao().loadFirstHomeSiteNewsClassByTypeDao(typeId);
	}

	public Map loadSecondHomeSiteNewsClassByType(Integer typeId, Long parenId) throws BusinessException 
	{
		return  this.getHomeSiteNewsClassDao().loadSecondHomeSiteNewsClassByTypeDao(typeId,  parenId);
	}

	/*****
	 *  获得广告类型下面的分类列表
	 */
	public Map loadSecondHomeSiteNewsClassByAdType(Long parenId) throws BusinessException 
	{
		//获取广告分类id
		BmClassType bmClassType = this.getBmClassTypeDao().getBmClassTypeByClassTypeName("广告类别");
		return this.loadSecondHomeSiteNewsClassByType(bmClassType.getId(), parenId);
	}

	public IBmClassTypeDao getBmClassTypeDao() {
		return bmClassTypeDao;
	}

	public void setBmClassTypeDao(IBmClassTypeDao bmClassTypeDao) {
		this.bmClassTypeDao = bmClassTypeDao;
	}

	/*****
	 * 通过分类id,获取广告描述列表
	 */
	public Map loadAdBannerListByHomeSiteNewsClassId(Long classId) throws BusinessException 
	{
		return  this.getHomeSiteNewsClassDao().loadAdBannerListByHomeSiteNewsClassIdDao(classId);
	}

	public Map<Long, String> loadSecondHomeSiteNewsClassByNewsType(Long parenId) throws BusinessException 
	{
//		获取广告分类id
		BmClassType bmClassType = this.getBmClassTypeDao().getBmClassTypeByClassTypeName("资讯类别");
		return this.loadSecondHomeSiteNewsClassByType(bmClassType.getId(), parenId);
	}

	/****
	 * 通过分类名称,获取分类对象
	 */
	public BmClassType loadBmNewsTypeByName(String name) throws BusinessException 
	{
		return this.getBmClassTypeDao().loadBmClassTypeByNameDao(name);
	}

	/*****
	 * 通过大分类id,获取下面的子分类列表 (用于市场行情这个栏目)
	 */
	public List<HomeSiteNewsClass> loadSCHQNewsClassList(Long id, Integer classTypeId, Integer top) throws BusinessException 
	{
		return  this.getHomeSiteNewsClassDao().loadSCHQNewsClassListDao(id,classTypeId,top);
	}

	
	/************
	 * 通过seq获取新闻分类列表
	 */
	public List<HomeSiteNewsClass> getHomeSiteNewsClassListBySeq(String seq) throws BusinessException {
		
		List<HomeSiteNewsClass> homeSiteNewsClssList = new ArrayList<HomeSiteNewsClass>();
		if(seq!=null  && !seq.equals("")){
			
			String[] newsClassIdS = seq.split(",");
			if(newsClassIdS!=null  && newsClassIdS.length>3){
				for(int j=3;j<newsClassIdS.length;j++){
					if(newsClassIdS[j]!=null  && !newsClassIdS[j].equals("") && !newsClassIdS[j].equals("-1")){
						HomeSiteNewsClass  homeSiteNewsClass = this.loadNewsClassById(Long.valueOf(newsClassIdS[j]));
						if(homeSiteNewsClass!=null){
							homeSiteNewsClssList.add(homeSiteNewsClass);
						}
					}
					 
				}
			}
		}
		
		return homeSiteNewsClssList;
		
	}
	
	
	/*******
	 * 通过股票代码,获取股票对象
	 * @param stockNumId
	 * @return
	 */
	public CmQuotedCompany loadCmQuotedCompanyByStockNumId(String stockNumId){
		return this.getCmQuotedCompanyDao().loadCmQuotedCompanyByStockNumId(stockNumId);
	}
	

	public ICmQuotedCompanyDao getCmQuotedCompanyDao() {
		return cmQuotedCompanyDao;
	}

	public void setCmQuotedCompanyDao(ICmQuotedCompanyDao cmQuotedCompanyDao) {
		this.cmQuotedCompanyDao = cmQuotedCompanyDao;
	}

	
	
}
